//-----------------------------------------------------------------------------
// Copyright (c) 2011 David Waby
// Initially created on 01/11/2011
//-----------------------------------------------------------------------------

#include "Global.h"
#include "World.h"
#include "Parser.h"
#include "Map.h"
#include "Character.h"
#include "EventManager.h"
#include "EventLoadMap.h"
	
//-----------------------------------------------------------------------------
World::World()
:	m_currentMap(NULL)
,	m_character(NULL)
{
	EventManager::Get().AddListener(this);
} 
	
//-----------------------------------------------------------------------------
World::~World()
{
	delete m_character;
	delete m_currentMap;
} 

//-----------------------------------------------------------------------------
void World::Initialise()
{
	Parser::ParseMapStore(&m_mapStore);
	Parser::ParseItemStore(&m_itemStore);

	m_character = new Character();
	m_character->Init();

	// LOAD THE FIRST LEVEL
	Point2 invalidLocation(-1, -1);
	EventManager::Get().PostEvent(EventLoadMap(0, &invalidLocation));
}

//-----------------------------------------------------------------------------
void World::Input()
{
	static int count = 0;

	if (count==0)
		m_character->Move(Direction::Right);
	else if (count==1)
		m_character->Move(Direction::Right);
	else if (count==2)
		m_character->Move(Direction::Up);
	else if (count==3)
		m_character->Use();
	else if (count==4)
		m_character->Move(Direction::Left);
	else if (count==5)
		m_character->Move(Direction::Down);
	else if (count==6)
		m_character->Move(Direction::Down);
	else if (count==7)
		m_character->Move(Direction::Right);
	else if (count==8)
		m_character->Move(Direction::Right);
	else if (count==9)
		m_character->Move(Direction::Up);
	else if (count==10)
		m_character->Move(Direction::Up);
	else if (count==11)
		m_character->Move(Direction::Left);
	else if (count==12)
		m_character->Use();
	else if (count==13)
		m_character->Move(Direction::Up);
	else if (count==14)
		m_character->Move(Direction::Left);
	else if (count==15)
		m_character->Move(Direction::Left);
	else if (count==16)
		m_character->Move(Direction::Down);
	else if (count==17)
		m_character->Move(Direction::Down);

	++count;
	if (count > 17)
		count = 0;
}

//-----------------------------------------------------------------------------
void World::Update()
{
}

//-----------------------------------------------------------------------------
void World::Render()
{
	m_renderer.RenderMap(m_currentMap, m_character);
}

//-----------------------------------------------------------------------------
void World::Shutdown()
{
}

//-----------------------------------------------------------------------------
void World::HandleEvent(Event const & i_event)
{
	switch (i_event.GetType())
	{
		case Event::EventType::LoadMap:
		{
			EventLoadMap const loadMapEvent = static_cast<EventLoadMap const&>(i_event);

			// Delete the current map
			delete m_currentMap;

			// Work out which map to load here
			Parser::ParseAndCreateMap(&m_currentMap, &m_mapStore.GetFilenameForID(loadMapEvent.GetMapID()));
			
			// Tell the character they have moved map
			m_character->SetMap(m_currentMap);

			// Set up the start position (if valid position then use it)
			if ((*loadMapEvent.GetStartPosition())[X] >= 0)
			{
				m_character->SetPosition(loadMapEvent.GetStartPosition());
			}
			else
			{
				// no start position so use the maps generic one
				m_character->SetPosition(m_currentMap->GetStartPosition());
			}
			
			break;
		}
		default:
			break;
	};
}
